slctVariable <- readRDS("./slctVariable.rds")
dat <- read.csv("./CleanedMediumData.csv",header=T)
head(dat)


rsquared <- function(y,f,n,p)
{
    ssreq <- sum( (f- mean(y))^2 )
    ssres <- sum( (y-f)^2 )
    sstot = ssreq+ssres
    dfe = n-p-1
    dft = n-1
  return ( 1-(ssres/dfe)/(sstot/dft))
}



trainSize = 0.7
library(caret)

iter=5
rmse.test <- matrix(ncol=1,nrow=iter)
rmse.train <- matrix(ncol=1,nrow=iter)
mape.test <- matrix(ncol=1, nrow=iter)
mape.train <- matrix(ncol=1, nrow=iter)
for (i in 1:iter)
{
    trainObs <- floor(trainSize*dim(dat)[1])
    testObs <- trainObs + 1
    idx <- sample(c(1:33),size=33,replace=F)
    trainIDX <- idx[1:trainObs]
    testingIDX <- idx[testObs:dim(dat)[1]]

    training <- dat[trainIDX,]
    testing <- dat[testingIDX,]

    training[,1:55] <- scale(training[,1:55])
    testing[,1:55] <- scale(testing[,1:55])

        
    x.test <- testing[,1:(ncol(training)-2)]
    y.test <- testing[,ncol(training)]
    x.train <- training[,1:(ncol(training)-2)]
    y.train <- training[,ncol(training)]

    PredModel <- caret::train(x=x.train, y=y.train, method="brnn",trControl= trainControl(method="cv"))
    PredModel

    yhat.test <- predict(PredModel, newdata=x.test)
    plot(yhat.test,y.test)
    abline(a=0,b=1)

    yhat.train <- predict(PredModel, newdata=x.train)
    
    rmse.test[i,1] <- sqrt(mean((yhat.test - y.test)^2))
    rmse.train[i,1] <- sqrt(mean((yhat.train - y.train)^2))
    mape.test[i,1] <- mean(abs(yhat.test - y.test)/y.test)
    mape.train[i,1] <-mean(abs(yhat.train - y.train)/y.train)
}

rmseDF <- data.frame(Train=rmse.train, Test=rmse.test)
png("ModelAccuracy.png")
boxplot(rmseDF,main="RMSE")
dev.off()


save.image("PredModel.RData")
